package com.dap.cms.governance.controller;

import com.dap.cms.governance.po.CmsManagerPo;
import com.dap.cms.governance.service.CmsManagerService;
import com.dap.cms.governance.service.CmsSysService;
import com.dap.cms.governance.service.CmsUserService;
import com.dap.cms.governance.vo.ManagerInfoVo;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 公共管理类
 * @author: tiger
 * @createTime:2018/5/7
 * @update:修改内容
 * @author: tiger
 * @updateTime:2018/5/7
 */
@Api(value = "/v1", description = "公共管理类接口")
@RestController
@RequestMapping("/v1")
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private CmsUserService cmsUserService;
    @Autowired
    private CmsSysService cmsSysService;
    @Autowired
    private CmsManagerService cmsManagerService;

    /**
     * 公司负责人账户注册
     */
    @PostMapping("/user/register")
    public Result register(@RequestParam(value = "name") String name, @RequestParam String password, @RequestParam String rePassword,
                           @RequestParam String telephone, @RequestParam String companyName, @RequestParam String sysName) {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(companyName) || StringUtils.isBlank(sysName) || StringUtils.
                isBlank(telephone) || StringUtils.isBlank(password) || StringUtils.isBlank(rePassword)) {
            return ResultUtils.error(400, "请求参数错误");
        }
        //查询公司名称和系统名称是否被占用，占用说明公司系统负责人已经注册
        Boolean flag = cmsSysService.check(companyName, sysName);
        if (flag) {
            return ResultUtils.error(500, "该公司和系统名称已经注册，请联系系统负责人申请账户");
        }
        if (!cmsUserService.checkName(name)) {
            return ResultUtils.error(500, "用户名已被占用，请重新输入用户名");
        }
        if (!password.equals(rePassword)) {
            return ResultUtils.error(400, "两次输入密码不一致");
        }

        cmsUserService.register(name, telephone, companyName, sysName, password);
        return ResultUtils.success(ResultEnum.CREATED, null);
    }

    /**
     * 校验系统是否已注册
     *
     * @param companyName
     * @param sysName
     * @return
     */
    @PostMapping("/register/sysName")
    public Result checkSysName(@RequestParam String companyName, @RequestParam String sysName) {
        Boolean flag = cmsSysService.check(companyName, sysName);
        return ResultUtils.success(flag);
    }

    /**
     * 校验用户名是否已注册
     *
     * @param name
     * @return
     */
    @PostMapping("/register/userName")
    public Result checkSysName(@RequestParam String name) {
        Boolean flag = cmsUserService.checkName(name);
        return ResultUtils.success(flag);
    }

    /**
     * 登录
     *
     * @param request
     * @param username 用户名
     * @param password 用户密码
     * @return
     */
    @ApiOperation(value = "管理员登录", httpMethod = "POST")
    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestParam("username") String username,
                        @RequestParam("password") String password) {
        Map<String, Object> map = new HashMap();
        HttpSession session = request.getSession(true);
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ResultUtils.error(400, "数据请求错误");
        }
        CmsManagerPo manager = cmsManagerService.login(username, password);
        if (manager != null) {
            ManagerInfoVo managerInfo = cmsManagerService.getManagerInfo(manager);
            session.setAttribute("user", manager);
            map.put("user", manager);
            map.put("managerInfo", managerInfo);
            return ResultUtils.success(map);
        } else {
            return ResultUtils.error(401, "用户名或密码错误");
        }
    }

    /**
     * 登出系统
     */
    @ApiOperation(value = "登出系统", httpMethod = "GET")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return ResultUtils.success();
    }

}
